﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Surveying;
using Point = System.Drawing.Point;
using System.Reflection;
using static System.Windows.Forms.AxHost;
using System.Windows.Forms.VisualStyles;
using System.Drawing.Drawing2D;
//using Surveying;
//using Point = Surveying.Point;

namespace GIS_SpatialAnalyst
{

    public partial class MainForm : Form
    {
        private double xmin;
        private double xmax;
        private double ymin;
        private double ymax;
        private double xrate;
        private double yrate;
        private Graphics grp;
        private Bitmap bmp;
        private Pen pen;
        private Font sfont;
        private string geoStyle;
        //Surveying.Point[] dpoint;
        //private List<Surveying.Point> VPoints;
        Queue queueTemp;
        private Surveying.Polygon m_Polygon;
        private Surveying.PolyLine m_Polyline;
        private List<Surveying.Point> m_Points;
        private Surveying.Line m_Line1;
        private Surveying.Line m_Line2;
        bool rightbutton = false;
        List<Triangle> delaunyTriangles = new List<Triangle>();
        List<Edge> useableEdges = new List<Edge>();
        List<Surveying.Point> tempPoints = new List<Surveying.Point>();
        private bool IsPLDraw=false;
        private bool IsPGDraw=false;

        public MainForm()
        {
            InitializeComponent();
            this.WindowState = FormWindowState.Maximized;
            btnClear.Click += new EventHandler(btnClear_Click);
            btnPolyline.Click += new EventHandler(btnPolyline_Click);
            btnPoint.Click += new EventHandler(btnPoint_Click);
            btnPolygon.Click += new EventHandler(btnPolygon_Click);
            btnLine.Click += new EventHandler(btnLine_Click);
            btnSX.Click += new EventHandler(btnSX_Click);
            btnYS.Click += new EventHandler(btnYS_Click);
            btnSLWJ.Click += new EventHandler(btnSLWJ_Click);
            Btn_CreateTin.Click += new EventHandler(GetBtn_CreateTin);
            btnCutLine.Click += new EventHandler(btnCutLine_Click);
            picDP.MouseMove += new MouseEventHandler(picDP_MouseMove);
            picDP.MouseDown += new MouseEventHandler(picDP_MouseDown);
            this.Load += new EventHandler(MainForm_Load);
            this.Resize += new EventHandler(MainForm_Resize);
        }

        private void GetBtn_CreateTin(object sender, EventArgs e)
        {
            if (m_Points.Count>=3)
            {
                Queue<Triangle> triqueue = new Queue<Triangle>();
              
                //寻找距离第一个点最近的点
                Surveying.Point bgnPoint = m_Points[0];
                for (int i = 0; i < m_Points.Count; i++)
                {
                    m_Points[i].id = i;
                    m_Points[i].disFromOrigin = m_Points[i].Distance(bgnPoint);
                }
                m_Points.Sort();
                Triangle delaunyTriangle = FindDelaunyTriangle(bgnPoint, m_Points[1],true);
                if (delaunyTriangle==null)
                {
                    delaunyTriangle= FindDelaunyTriangle(m_Points[1], bgnPoint, true);
                }
                    triqueue.Enqueue(delaunyTriangle);
                    while (triqueue.Count > 0)
                    {
                        var temptri = triqueue.Dequeue();
                        delaunyTriangles.Add(temptri);
                        //DrawCicle(temptri);
                        DrawTriangle(temptri);
                        foreach (var edge in temptri.edges)
                        {  
                            if (edge.useful)
                            {
                                delaunyTriangle = FindDelaunyTriangle(edge.point_one, edge.point_two, false);
                                if (delaunyTriangle != null)
                                {
                                    triqueue.Enqueue(delaunyTriangle);
                                }
                            }
                          
                        }
                    }
                //}
             
            }

        }
        /// <summary>
        /// 获取满足空外接圆的三角形，顺时针方向构建三角形，逆时针方向找点（第一次除外）
        /// </summary>
        /// <param name="orignPoint"></param>
        /// <param name="pLR"></param>
        /// <returns></returns>
        private Triangle FindDelaunyTriangle(Surveying.Point orignPoint, Surveying.Point sourcePoint,bool first)
        {
            var pLR = new Point_Line_RelationClass();
            List<Surveying.Point> oneSidePoints = new List<Surveying.Point>();
            List<Triangle> oneSideTriangles = new List<Triangle>();
            int ckock_direction;
            if (first)
            {
                for (int i = 2; i < m_Points.Count; i++)
                {
                    ckock_direction = pLR.get_Relation_Point_Line(m_Points[i], orignPoint, sourcePoint);
                    if (ckock_direction == -1)
                    {
                        oneSidePoints.Add(m_Points[i]);
                        //顺时针方向构建三角形
                        Triangle triangle = new Triangle(orignPoint, sourcePoint, m_Points[i]);
                        oneSideTriangles.Add(triangle);
                    }
                }
            }
            else
            {
                for (int i = 2; i < m_Points.Count; i++)
                {
                    if (m_Points[i]==orignPoint|| m_Points[i]==sourcePoint)
                    {
                        continue;
                    }
                    ckock_direction = pLR.get_Relation_Point_Line(m_Points[i], orignPoint, sourcePoint);
                    if (ckock_direction == 1)
                    {
                        oneSidePoints.Add(m_Points[i]);
                        //逆时针时针方向构建三角形
                        Triangle triangle = new Triangle(orignPoint, m_Points[i], sourcePoint);
                        oneSideTriangles.Add(triangle);
                    }
                }
            }
            bool inside;
            Triangle stardTriangle = null;
            //利用delauny法则三角形来构建delauny三角形
            for (int i = 0; i < oneSideTriangles.Count; i++)
            {
                inside = false;
                for (int j = 0; j < oneSidePoints.Count; j++)
                {
                    inside = oneSideTriangles[i].Circumcircle.Is_PointinCicle(oneSidePoints[j]);
                    if (inside)
                    {
                        oneSideTriangles[i].stard = false;
                        break;
                    }
                }
                if (oneSideTriangles[i].stard)
                {
                    stardTriangle = oneSideTriangles[i];
                  
                    if (!first)
                    {
                        foreach (var edge in stardTriangle.edges)
                        {
                            useableEdges.Add(edge);
                            DisableEqualEdges(useableEdges);
                        }
                    }
                    else
                    {
                        foreach (var edge in stardTriangle.edges)
                        {
                            useableEdges.Add(edge);
                        }
                    }
                    break;
                }
            }
            return stardTriangle;
        }

        private static void DisableEqualEdges(List<Edge> usedEdges)
        {
            List<Edge> equalEdges = usedEdges.FindAll(o => o.EqualWith(usedEdges.Last()));
            if (equalEdges.Count == 2)
            {
                equalEdges.ForEach(o => o.useful = false);
                usedEdges.Remove(equalEdges[0]);
                usedEdges.Remove(equalEdges[1]);
            }
        }

        private void DrawCicle(Triangle triangle)
        {
            double x = triangle.Circumcircle.center.x;
            double y = picDP.Height - triangle.Circumcircle.center.y;
            double radius = triangle.Circumcircle.radius;
            //Pen blackPen = new Pen(Color.Black, 3);
            grp.DrawEllipse(Pens.Blue, (float)(x - radius), (float)(y - radius), (float)(radius * 2), (float)(radius * 2));
            picDP.Image = bmp;
        }

        private void DrawTriangle(Triangle triangle)
        {
            PointF[] points = new PointF[3];
            points[0] = new PointF((float)triangle.points[0].x, (float)(picDP.Height - triangle.points[0].y));
            points[1] = new PointF((float)(triangle.points[1].x), (float)(picDP.Height - triangle.points[1].y));
            points[2] = new PointF((float)(triangle.points[2].x), (float)(picDP.Height - triangle.points[2].y));
            grp.DrawPolygon(Pens.Red, points);
            picDP.Image = bmp;
        }

        void MainForm_Resize(object sender, EventArgs e)
        {
            xrate = (xmax - xmin) / picDP.Height;
            yrate = (ymax - ymin) / picDP.Width;           
        }

        void picDP_MouseDown(object sender, MouseEventArgs e)
        {
            if (geoStyle == "Polygon")
            {
                Polygon_MouseDown(sender, e);
            }
            else if (geoStyle == "Line")
            {
                Line_MouseDown(sender, e);
            }
            else if (geoStyle == "Polyline")
            {
                Polyline_MouseDown(sender, e);
            }
            else if (geoStyle == "Point")
            {
                Point_MouseDown(sender, e);
            }
        }

        void picDP_MouseMove(object sender, MouseEventArgs e)
        {
            if (geoStyle == "Polygon")
            {
                Polygon_MouseMove(sender, e);
            }           
            else if (geoStyle == "Polyline")
            {
                Polyline_MouseMove(sender, e);
            }
            else if (geoStyle == "Point")
            {
                Point_MouseMove(sender, e);
            }
        }

        void MainForm_Load(object sender, EventArgs e)
        {
            xmin = 0;
            ymin = 0;
            xmax = 100;
            ymax = 150;
            xrate = (xmax - xmin) / picDP.Height;
            yrate = (ymax - ymin) / picDP.Width;
            bmp = new Bitmap(picDP.Width, picDP.Height);
            grp = Graphics.FromImage(bmp);
            grp.Clear(picDP.BackColor);
            pen = new Pen(Color.Black, 1);
            sfont = new Font("宋体", 12);
            queueTemp = new Queue();
            m_Polygon = new Surveying.Polygon();
            m_Polyline = new Surveying.PolyLine();
            rightbutton = false;      
            m_Points = new List<Surveying.Point>();
            m_Polygon = new Surveying.Polygon();
            m_Polyline = new Surveying.PolyLine();
            m_Line1 = new Surveying.Line();
            m_Line2 = new Surveying.Line();
            //btnCutLine.Visible=false;
            //btnSX.Visible=false;
            //btnSLWJ.Visible=false;
            //btnYS.Visible=false;
            //Btn_CreateTin.Visible=false;
            //btnCutLine.Visible = false;
        }

        void btnCutLine_Click(object sender, EventArgs e)
        {
            var Pgc=new PolygonClipClass(picDP,grp,bmp);
            Pgc.Clip(m_Polygon, m_Polyline);
        }

        void btnSLWJ_Click(object sender, EventArgs e)
        {
           var lLR =  new Line_Line_RelationClass();
            lLR.p1 = m_Line1.point_one;
            lLR.p2 = m_Line1.point_two;
            lLR.p3 = m_Line2.point_one;
            lLR.p4 = m_Line2.point_two;
            Surveying.frmMessage newForm = new Surveying.frmMessage();
            if (lLR.CrossVector())
            {
                string pInfo = "这两条线相交";
                newForm.Info = pInfo;
                newForm.Show();
            }
            else
            {
                string pInfo = "这两条线不相交";
                newForm.Info = pInfo;
                newForm.Show();
            }
          
        }

        void btnYS_Click(object sender, EventArgs e)
        {
            var pPL = new Surveying.Point_Line_RelationClass();
            string pInfo = "";
            for (int i = 0; i < m_Points.Count; i++)
            {
                pPL.VPoint = m_Points[i];
                pPL.LinePoint_1 = m_Line1.point_one;
                pPL.LinePoint_2 = m_Line1.point_two;
                string ret = pPL.RelationCal();
                pInfo = pInfo + "点号" + i.ToString() + ": " + ret + "\r\n" + "\r\n";
            }
            Surveying.frmMessage newForm = new Surveying.frmMessage();
            newForm.Info = pInfo;
            newForm.Show();
        }

        void btnSX_Click(object sender, EventArgs e)
        {
          var pPC=  new Point_PolygonClass();
            string pInfo = "";
            string ret = "";
            for (int i = 0; i < m_Points.Count; i++)
            {
                m_Points[i].whetherIn=  pPC.Is_Point_in_Polygon(m_Polygon, m_Points[i]);
                if (m_Points[i].whetherIn)
                {
                    ret = "在多边形内";
                }
                else
                {
                    ret = "在多边形外";
                }
                pInfo = pInfo + "点号" + i.ToString() + ": " + ret + "\r\n" + "\r\n";
            }
            Surveying.frmMessage newForm = new Surveying.frmMessage();
            newForm.Info = pInfo;
            newForm.Show();
        }

        void btnLine_Click(object sender, EventArgs e)
        {
            queueTemp.Clear();
            geoStyle = "Line";
        }

        void btnPolygon_Click(object sender, EventArgs e)
        {
            tempPoints.Clear();
            geoStyle = "Polygon";
        }

        void btnPoint_Click(object sender, EventArgs e)
        {
            queueTemp.Clear();
            geoStyle = "Point";
        }

        void btnPolyline_Click(object sender, EventArgs e)
        {
            tempPoints.Clear();
            geoStyle = "Polyline";
        }

        void btnClear_Click(object sender, EventArgs e)
        {
            m_Polygon = new Surveying.Polygon();
            m_Polyline = new Surveying.PolyLine();
            rightbutton = false;
            queueTemp.Clear();
            m_Points = new List<Surveying.Point>();
            m_Polygon = new Surveying.Polygon();
            m_Polyline = new Surveying.PolyLine();
            m_Line1 = new Surveying.Line();
            grp.Clear(picDP.BackColor);
            picDP.Refresh();
        }

        void DrawPolygon()
        {
            if (m_Polygon.point_count > 0)
            {
                int i;
                Point[] points = new Point[m_Polygon.points.Length - 1];
                for (i = 0; i < m_Polygon.points.Length - 1; i++)
                {
                    points[i] = new Point();
                    points[i].X = Convert.ToInt32(m_Polygon.points[i + 1].x);
                    points[i].Y = Convert.ToInt32(picDP.Height - m_Polygon.points[i + 1].y);
                }
                //grp.DrawPolygon(Pens.Black, points);
                grp.FillPolygon(new SolidBrush(Color.Yellow), points);
                for (i = 0; i < m_Polygon.points.Length - 1; i++)
                {
                    grp.DrawString(Convert.ToString(i), sfont, Brushes.Red, Convert.ToSingle(m_Polygon.points[i + 1].x), Convert.ToSingle(picDP.Height - m_Polygon.points[i + 1].y));
                }
            }
        }

        void DrawPolyLine()
        {
            if (m_Polyline.point_count > 0)
            {
                int i;
                for (i = 1; i <= m_Polyline.point_count - 1; i++)
                {
                    grp.DrawString(Convert.ToString(i), sfont, Brushes.Red, Convert.ToSingle(m_Polyline.point[i].x), Convert.ToSingle(picDP.Height - m_Polyline.point[i].y));
                    grp.DrawLine(new Pen(Color.Blue, 2), Convert.ToSingle(m_Polyline.point[i].x), Convert.ToSingle(picDP.Height - m_Polyline.point[i].y), Convert.ToSingle(m_Polyline.point[i + 1].x), Convert.ToSingle(picDP.Height - m_Polyline.point[i + 1].y));
                }
                grp.DrawString(Convert.ToString(i), sfont, Brushes.Red, Convert.ToSingle(m_Polyline.point[i].x), Convert.ToSingle(picDP.Height - m_Polyline.point[i].y));
            }
        }
        
        void DrawPoints()
        {
            for (int i = 0; i < m_Points.Count; i++)
            {
                grp.DrawRectangle(new Pen(Color.Blue, 3), Convert.ToSingle(m_Points[i].x), Convert.ToSingle(picDP.Height - m_Points[i].y), 3, 3);
                grp.DrawString(Convert.ToString(i), sfont, Brushes.Blue, Convert.ToSingle(m_Points[i].x + 5), Convert.ToSingle(picDP.Height - m_Points[i].y + 5));
            }
        }
        void DrawPoint( Surveying.Point point,int i)
        {
                point.id = i;
                grp.DrawRectangle(new Pen(Color.Blue, 3), Convert.ToSingle(point.x), Convert.ToSingle(picDP.Height - point.y), 3, 3);
                grp.DrawString(Convert.ToString(i), sfont, Brushes.Blue, Convert.ToSingle(point.x + 5), Convert.ToSingle(picDP.Height - point.y + 5));
        }
        void DrawLine()
        {
            if (m_Line1!=null && m_Line1.point_one != null && m_Line1.point_two != null)
            {
                grp.DrawString("1", sfont, Brushes.Red, Convert.ToSingle(m_Line1.point_one.x), Convert.ToSingle(picDP.Height - m_Line1.point_one.y));
                grp.DrawString("2", sfont, Brushes.Red, Convert.ToSingle(m_Line1.point_two.x), Convert.ToSingle(picDP.Height - m_Line1.point_two.y));
                grp.DrawLine(new Pen(Color.Blue, 2), Convert.ToSingle(m_Line1.point_one.x), Convert.ToSingle(picDP.Height - m_Line1.point_one.y), Convert.ToSingle(m_Line1.point_two.x), Convert.ToSingle(picDP.Height - m_Line1.point_two.y));
            }
        }

        private void CalCoorXY(int picCoorX, int picCoorY, ref double X, ref double Y)
        {
            X = (-picCoorY + picDP.Height) * xrate;
            Y = picCoorX * yrate;
        }
        private void CalPicCoorXY(double X, double Y, ref Single picCoorX, ref Single picCoorY)
        {
            picCoorX = Convert.ToSingle(Y / yrate);
            picCoorY = Convert.ToSingle(picDP.Height - X / xrate);
        }

        /// <summary>
        /// 画多边形的MouseDown事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Polygon_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                int i = 0;
                Surveying.Point pointemp = new Surveying.Point();
                pointemp.x = e.X;
                pointemp.y = -e.Y + picDP.Height;
                tempPoints.Add(pointemp);
                if (tempPoints.Count >= 2)
                {
                    i = tempPoints.Count - 1;
                    float x;
                    float y;
                    x = (float)tempPoints[i].x;
                    y = (float)(picDP.Height - tempPoints[i].y);
                    if (tempPoints.Count == 2)
                    {
                        grp.DrawString(0.ToString(), sfont, Brushes.Red, (float)tempPoints[0].x, (float)(picDP.Height - tempPoints[0].y));
                    }
                    //grp.DrawLine(new Pen(Color.Blue, 2), (float)tempPoints[i - 1].x, (float)(picDP.Height - tempPoints[i - 1].y), x, y);
                    grp.DrawString(i.ToString(), sfont, Brushes.Red, x, y);
                    picDP.Image = bmp;
                }
            }
            //右键结束绘图
            else
            {
                rightbutton = true;
                m_Polygon = new Polygon(tempPoints.ToArray());
                tempPoints.Clear();
                int i;
                Point[] points = new Point[m_Polygon.points.Length-1];
                for (i = 0; i < m_Polygon.points.Length-1; i++)
                {
                    points[i] = new Point();
                    points[i].X = Convert.ToInt32(m_Polygon.points[i ].x);
                    points[i].Y = Convert.ToInt32(picDP.Height - m_Polygon.points[i ].y);
                }
                grp.FillPolygon(new SolidBrush(Color.FromArgb(1, Color.Yellow)), points);
                picDP.Image = bmp;
                IsPGDraw = true;
            }
        }

        /// <summary>
        /// 画复合线的MouseDown事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Polyline_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                int i = 0;
                Surveying.Point pointemp = new Surveying.Point();
                pointemp.x = e.X;
                pointemp.y = -e.Y + picDP.Height;
                tempPoints.Add(pointemp);
                if (tempPoints.Count >= 2)
                {
                    i = tempPoints.Count-1;
                    float x;
                    float y;
                    x = (float)tempPoints[i ].x;
                    y = (float)(picDP.Height - tempPoints[i ].y);
                    if (tempPoints.Count == 2)
                    {
                        grp.DrawString(0.ToString(), sfont, Brushes.Red,(float) tempPoints[0].x,(float)(picDP.Height - tempPoints[0].y));
                    }
                    grp.DrawLine(new Pen(Color.Blue, 2), (float)tempPoints[i - 1].x, (float)(picDP.Height - tempPoints[i - 1].y), x, y);
                    grp.DrawString(i.ToString(), sfont, Brushes.Red, x, y);
                    picDP.Image = bmp;
                }
            }
            //右键的情况
            else
            {
                m_Polyline = new PolyLine(tempPoints.ToArray());
                tempPoints.Clear();
                IsPLDraw = true;
                if (IsPGDraw&&IsPLDraw)
                {
                    btnCutLine.Visible = true;
                }
            }
        }

        /// <summary>
        /// 画点的MouseDown事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Point_MouseDown(object sender, MouseEventArgs e)
        {
            Surveying.Point pointemp = new Surveying.Point();
            pointemp.x = e.X;
            pointemp.y = -e.Y + picDP.Height;
            m_Points.Add(pointemp);
            DrawPoint(pointemp, m_Points.Count-1);
            //UpdateTin(pointemp);
            picDP.Image = bmp;
        }

        private void UpdateTin(Surveying.Point pointemp)
        {
            if (useableEdges.Count>0)
            {
                bool IsPointOut = false;
                Cicle tempCicle=new Cicle();
                Triangle tempTriangle = null;
                Line tempLine = null;
                for (int i = 0; i < delaunyTriangles.Count; i++)
                {
                    if (Point_PolygonClass.PointInFences(pointemp, delaunyTriangles[i]))
                    {
                        foreach (var edge in delaunyTriangles[i].edges)
                        {
                            tempCicle = edge.EqualEdge.ownerTriangle.Circumcircle;
                            if (tempCicle.Is_PointinCicle(pointemp))
                            {

                            }
                            var newTriangle = new Triangle(edge.point_two, pointemp, edge.point_one);
                            delaunyTriangles.Add(newTriangle);
                            DrawTriangle(newTriangle);
                        }
                        break;
                    }
                    else
                    {
                        IsPointOut = true;
                    }
                }
                if (IsPointOut)
                {
                    for (int i = 0; i < useableEdges.Count; i++)
                    {
                        tempTriangle = useableEdges[i].ownerTriangle;
                        tempCicle = tempTriangle.Circumcircle;
                      
                        if (tempCicle.Is_PointinCicle(pointemp))
                        {
                            var thirdPoint = tempTriangle.points.Where(o => o != useableEdges[i].point_one && o != useableEdges[i].point_two).ToArray();

                            var newTriangle1 = new Triangle(pointemp, useableEdges[i].point_one, thirdPoint[0]);
                            var newTriangle2= new Triangle(pointemp, useableEdges[i].point_two, thirdPoint[0]);
                            DrawTriangle(newTriangle1);
                            DrawTriangle(newTriangle2);
                            grp.DrawLine(new Pen(Color.White, 2), (float)(useableEdges[i].point_one.x), (float)(picDP.Height-useableEdges[i].point_one.y), (float)(useableEdges[i].point_two.x), (float)(picDP.Height - useableEdges[i].point_two.y));
                            picDP.Image = bmp;
                            break;
                        }
                        else
                        {
                            var newTriangle = FindDelaunyTriangle(useableEdges[i].point_one, useableEdges[i].point_two, false);
                            if (newTriangle != null)
                            {
                                DrawTriangle(newTriangle);
                            }
                        }
                       
                    }
                }
            }
           
          
        }

        /// <summary>
        /// 画线段的MouseDown事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Line_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (queueTemp.Count > 8) 
                    return;
                grp.Clear(picDP.BackColor);
                int i = 0;
                DrawPolygon();
                DrawPolyLine();
                DrawPoints();

                rightbutton = false;
                Surveying.Point pointemp = new Surveying.Point();
                Surveying.Point[] p;
                pointemp.x = e.X;
                pointemp.y = -e.Y + picDP.Height;
                queueTemp.Enqueue(pointemp.x);
                queueTemp.Enqueue(pointemp.y);
                if (queueTemp.Count >= 4)
                {
                    i = 0;
                    p = new Surveying.Point[queueTemp.Count / 2 + 1];
                    while (queueTemp.Count > 0)
                    {
                        i = i + 1;
                        p[i] = new Surveying.Point();
                        p[i].x = Convert.ToDouble(queueTemp.Dequeue());
                        p[i].y = Convert.ToDouble(queueTemp.Dequeue());
                    }
                    for (i = 1; i <= p.Length - 1; i++)
                    {
                        queueTemp.Enqueue(p[i].x);
                        queueTemp.Enqueue(p[i].y);
                    }
                    m_Line1.point_one = p[1];
                    m_Line1.point_two = p[2];
                    grp.DrawString("1", sfont, Brushes.Red, Convert.ToSingle(m_Line1.point_one.x), Convert.ToSingle(picDP.Height - m_Line1.point_one.y));
                    grp.DrawString("2", sfont, Brushes.Red, Convert.ToSingle(m_Line1.point_two.x), Convert.ToSingle(picDP.Height - m_Line1.point_two.y));
                    grp.DrawLine(new Pen(Color.Blue, 2), Convert.ToSingle(m_Line1.point_one.x), Convert.ToSingle(picDP.Height - m_Line1.point_one.y), Convert.ToSingle(m_Line1.point_two.x), Convert.ToSingle(picDP.Height - m_Line1.point_two.y));
                    picDP.Image = bmp;
                }
                if (queueTemp.Count >= 8)
                {
                    i = 0;
                    p = new Surveying.Point[queueTemp.Count / 2 + 1];
                    while (queueTemp.Count > 0)
                    {
                        i = i + 1;
                        p[i] = new Surveying.Point();
                        p[i].x = Convert.ToDouble(queueTemp.Dequeue());
                        p[i].y = Convert.ToDouble(queueTemp.Dequeue());
                    }
                    for (i = 1; i <= p.Length - 1; i++)
                    {
                        queueTemp.Enqueue(p[i].x);
                        queueTemp.Enqueue(p[i].y);
                    }
                    m_Line1.point_one = p[1];
                    m_Line1.point_two = p[2];
                    m_Line2.point_one = p[3];
                    m_Line2.point_two = p[4];
                    grp.DrawString("3", sfont, Brushes.Red, Convert.ToSingle(m_Line2.point_one.x), Convert.ToSingle(picDP.Height - m_Line2.point_one.y));
                    grp.DrawString("4", sfont, Brushes.Red, Convert.ToSingle(m_Line2.point_two.x), Convert.ToSingle(picDP.Height - m_Line2.point_two.y));
                    grp.DrawLine(new Pen(Color.Blue, 2), Convert.ToSingle(m_Line2.point_one.x), Convert.ToSingle(picDP.Height - m_Line2.point_one.y), Convert.ToSingle(m_Line2.point_two.x), Convert.ToSingle(picDP.Height - m_Line2.point_two.y));
                    picDP.Image = bmp;
                }
            }
        }

        /// <summary>
        /// 画多边形的MouseMove事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Polygon_MouseMove(object sender, MouseEventArgs e)
        {
            if (rightbutton == false)
            {
                if (tempPoints.Count >= 2)
                {
                    int i = 0;
                    Point[] points = new Point[tempPoints.Count];
                    for (i = 0; i <= tempPoints.Count - 1; i++)
                    {
                        points[i] = new Point();
                        points[i].X = Convert.ToInt32(tempPoints[i].x);
                        points[i].Y = Convert.ToInt32(picDP.Height - tempPoints[i].y);
                    }
                    grp.FillPolygon(new SolidBrush(Color.FromArgb(1, Color.Yellow)), points);
                    picDP.Image = bmp;
                }
            }
        }

        /// <summary>
        /// 画复合线的MouseMove事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Polyline_MouseMove(object sender, MouseEventArgs e)
        {
            //double X, Y;
            //X = 0;
            //Y = 0;

            //labCoor.Left = e.X + 15;
            //labCoor.Top = e.Y - 10;

            //this.CalCoorXY(e.X, e.Y, ref X, ref Y);
            //labCoor.Text = e.X.ToString("0.000") + "," + (-e.Y + picDP.Height).ToString("0.000");
            //if (rightbutton == false)
            //{
                //if (tempPoints.Count >= 2)
                //{
                //    Surveying.Point lastPoint=tempPoints[tempPoints.Count - 1];
                    //Surveying.Point firstPoint=tempPoints[tempPoints.Count - 2];
                    //grp.Clear(picDP.BackColor);
                    //DrawPolygon();
                    //DrawLine();
                    //DrawPoints();

                    //int i = 0;
                    //for (i = 1; i <= m_Polyline.point_count - 1; i++)
                    //{
                    //    grp.DrawString(Convert.ToString(i), sfont, Brushes.Red, Convert.ToSingle(m_Polyline.point[i].x), Convert.ToSingle(picDP.Height - m_Polyline.point[i].y));
                    //    grp.DrawLine(new Pen(Color.Blue, 2), Convert.ToSingle(m_Polyline.point[i].x), Convert.ToSingle(picDP.Height - m_Polyline.point[i].y), Convert.ToSingle(m_Polyline.point[i + 1].x), Convert.ToSingle(picDP.Height - m_Polyline.point[i + 1].y));
                    //}
                    //grp.DrawString(Convert.ToString(i), sfont, Brushes.Red, Convert.ToSingle(m_Polyline.point[i].x), Convert.ToSingle(picDP.Height - m_Polyline.point[i].y));
                    //grp.DrawLine(new Pen(Color.Blue, 2), (float)(lastPoint.x), (float)(picDP.Height - lastPoint.y), (float)(e.X), (float)(e.Y));
                    //picDP.Image = bmp;
                //}
            //}
        }


        void Point_MouseMove(object sender, MouseEventArgs e)
        {
            double X, Y;
            X = 0;
            Y = 0;

            labCoor.Left = e.X + 15;
            labCoor.Top = e.Y - 10;

            this.CalCoorXY(e.X, e.Y, ref X, ref Y);
            labCoor.Text = e.X.ToString("0.000") + "," + (-e.Y + picDP.Height).ToString("0.000");
        }

    }
}
